package tt.dz.mvc.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import tt.dz.dao.ext.charger.OmsChargerDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.charger.OmsCharger;
import tt.dz.entity.dic.OmsDicForwardSrvInfo;
import tt.dz.service.sys.ChargerService;
import tt.dz.service.sys.OmsDicForwardSrvInfoService;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;

/**
 * oms_dic_forward_srv_info[桩硬件服务器信息]
 * @author pengyu
 *
 */
@Controller
@RequestMapping("dicForwardSrvInfoApi")
public class DicForwardSrvInfoAction extends BaseAction
{
	@Autowired
	private OmsDicForwardSrvInfoService omsDicForwardSrvInfoServiceImpl;
	@Autowired
	private ChargerService chargerServiceImpl;
	@Autowired
	private OmsChargerDao omsChargerDao;
	
	@RequestMapping(value="getList",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> getList(HttpServletRequest request, HttpServletResponse response)
	{
		List<OmsDicForwardSrvInfo> list = omsDicForwardSrvInfoServiceImpl.findList(new OmsDicForwardSrvInfo(), null, null, null, null);
		JsonForList<OmsDicForwardSrvInfo> json  = new JsonForList<OmsDicForwardSrvInfo>(list, "200", "query success");
		return json;
	}
	
	@RequestMapping(value="save",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> save(HttpServletRequest request, HttpServletResponse response)
	{
		String main_hardware_ip = request.getParameter("main_hardware_ip");
		String main_port = request.getParameter("main_port_up");
		String back_headware_ip = request.getParameter("back_headware_ip");
		String back_port_up = request.getParameter("back_port_up");
		String capacity = request.getParameter("capacity");
		String current_capacity = request.getParameter("current_capacity");
		String forward_srv_name = request.getParameter("forward_srv_name");
		
		String back_port_down = request.getParameter("back_port_down");
		String main_port_down = request.getParameter("main_port_down");
		String valid_flag = request.getParameter("valid_flag");
		
		OmsDicForwardSrvInfo dic = new OmsDicForwardSrvInfo();
		dic.setBack_port_down(Integer.valueOf(back_port_down));;
		dic.setMain_port_down(Integer.valueOf(main_port_down));
		dic.setValid_flag(Integer.valueOf(valid_flag));
		dic.setForward_srv_name(forward_srv_name);
		dic.setMain_hardware_ip(main_hardware_ip);
		dic.setMain_port_up(Integer.valueOf(main_port));
		dic.setBack_headware_ip(back_headware_ip);
		dic.setBack_port_up(Integer.valueOf(back_port_up));
		dic.setCapacity(Integer.valueOf(capacity));
		dic.setCurrent_capacity(Integer.valueOf(current_capacity));
		omsDicForwardSrvInfoServiceImpl.save(dic);
		return OmsConstant.jsonSuccess;
	}
	
	@RequestMapping(value="update",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> update(HttpServletRequest request, HttpServletResponse response)
	{
		String forward_srv_id = request.getParameter("forward_srv_id");
		OmsDicForwardSrvInfo oms_query = new OmsDicForwardSrvInfo();
		oms_query.setForward_srv_id(Integer.valueOf(forward_srv_id));
		OmsDicForwardSrvInfo  info = omsDicForwardSrvInfoServiceImpl.findOneExample(oms_query, null, null);
		if(info == null){
			JsonCommon<String> json = new JsonCommon<String>();
			json.setCode("400");
			json.setMsg("forward_srv_id : "+forward_srv_id+" not in database");
			return json;
		}
		String forward_srv_name = request.getParameter("forward_srv_name");
		String main_hardware_ip = request.getParameter("main_hardware_ip");
		String main_port_up = request.getParameter("main_port_up");
		String back_headware_ip = request.getParameter("back_headware_ip");
		String back_port_up = request.getParameter("back_port_up");
		String current_capacity = request.getParameter("current_capacity");
		String capacity = request.getParameter("capacity");
		String back_port_down = request.getParameter("back_port_down");
		String main_port_down = request.getParameter("main_port_down");
		String valid_flag = request.getParameter("valid_flag");
		info.setBack_port_down(Integer.valueOf(back_port_down));;
		info.setMain_port_down(Integer.valueOf(main_port_down));
		info.setValid_flag(Integer.valueOf(valid_flag));
		info.setForward_srv_name(forward_srv_name);
		info.setMain_hardware_ip(main_hardware_ip);
		info.setMain_port_up(Integer.valueOf(main_port_up));
		info.setBack_headware_ip(back_headware_ip);
		info.setBack_port_up(Integer.valueOf(back_port_up));
		info.setCapacity(Integer.valueOf(capacity));
		info.setCurrent_capacity(Integer.valueOf(current_capacity));
		omsDicForwardSrvInfoServiceImpl.updateObject(info);
		return OmsConstant.jsonSuccess;
	}
	
	@RequestMapping(value="logicDelete",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> logicDelete(HttpServletRequest request, HttpServletResponse response)
	{
		String forward_srv_id = request.getParameter("forward_srv_id");
		OmsDicForwardSrvInfo oms_query = new OmsDicForwardSrvInfo();
		oms_query.setForward_srv_id(Integer.valueOf(forward_srv_id));
		OmsDicForwardSrvInfo  info = omsDicForwardSrvInfoServiceImpl.findOneExample(oms_query, null, null);
		if(info == null){
			JsonCommon<String> json = new JsonCommon<String>();
			json.setCode("400");
			json.setMsg("forward_srv_id : "+forward_srv_id+" not in database");
			return json;
		}
//		info.setValid_flag(0);
		omsDicForwardSrvInfoServiceImpl.logicDelete(info);
		return OmsConstant.jsonSuccess;
	}
	
	@RequestMapping(value="bindChargers",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> bindChargers(HttpServletRequest request, HttpServletResponse response)
	{
		String forward_srv_id = request.getParameter("forward_srv_id");
		String chargerId_str = request.getParameter("chargerIds");
		List<String> chargerIdList =  JSON.parseArray(chargerId_str,String.class);
//		String[] urlIds = (String[]) urlList.toArray(new String[0]);
		omsDicForwardSrvInfoServiceImpl.bindChargers(forward_srv_id, chargerIdList);
		return OmsConstant.jsonSuccess;
	}
	
	@RequestMapping(value="unBindChargers",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> unBindChargers(HttpServletRequest request, HttpServletResponse response)
	{
		String chargerId_str = request.getParameter("chargerIds");
		List<String> chargerIdList =  JSON.parseArray(chargerId_str,String.class);
//		String[] urlIds = (String[]) urlList.toArray(new String[0]);
		omsDicForwardSrvInfoServiceImpl.unBindChargers(chargerIdList);
		return OmsConstant.jsonSuccess;
	}
	
	
	//根据硬件服务器ID，查询已经绑定的电桩和未绑定其他硬件服务器的电桩，用is_forward_srv_id标示，1为已经绑定的，0位未绑定的。
	
	//查询硬件服务器ID绑定的桩列表
	@RequestMapping(value="findChargersByForwardId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findChargersByForwardId(HttpServletRequest request, HttpServletResponse response)
	{
		
		String forward_srv_id = request.getParameter("forward_srv_id");
//		Integer pageNo, Integer pageSize,String sortName, Integer sortVal
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		String sortName = request.getParameter("sortName");
		String sortVal = request.getParameter("sortVal");
//		OmsDicForwardSrvInfo omsDicForwardSrvInfo = new OmsDicForwardSrvInfo();
		if(!StringUtil.isNumeric(forward_srv_id)){//MissParam("10303101", "缺少参数字段"),
			return new JsonForList<>(null, "10303101", "缺少参数字段");
		}
//		omsDicForwardSrvInfo.setForward_srv_id(Integer.valueOf(forward_srv_id));
//		List<OmsDicForwardSrvInfo> list = omsDicForwardSrvInfoServiceImpl.findList(omsDicForwardSrvInfo, 
//				StringUtil.isNumeric(pageNo)?Integer.valueOf(pageNo):null, 
//				StringUtil.isNumeric(pageSize)?Integer.valueOf(pageSize):null, 
//				StringUtil.isNotBlank(sortName)?sortName:null, 
//				StringUtil.isNumeric(sortVal)?Integer.valueOf(sortVal):null);
//		
		OmsCharger charger_query = new OmsCharger();
		charger_query.setForward_srv_id(Integer.valueOf(forward_srv_id));
		List<OmsCharger> list = chargerServiceImpl.findExample(charger_query, 
				StringUtil.isNumeric(pageNo)?Integer.valueOf(pageNo):null, 
				StringUtil.isNumeric(pageSize)?Integer.valueOf(pageSize):null, 
				StringUtil.isNotBlank(sortName)?sortName:null, 
				StringUtil.isNumeric(sortVal)?Integer.valueOf(sortVal):null);
		JsonForList<?> json  = new JsonForList<>(list, "200", "query success");
		return json;
	}
	//查询未绑定硬件服务器的桩列表
	@RequestMapping(value="findNullChargers",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findNullChargers(HttpServletRequest request, HttpServletResponse response)
	{
		
//		String forward_srv_id = request.getParameter("forward_srv_id");
//			Integer pageNo, Integer pageSize,String sortName, Integer sortVal
		String pageNo = request.getParameter("pageNo");
		String pageSize = request.getParameter("pageSize");
		String sortName = request.getParameter("sortName");
		String sortVal = request.getParameter("sortVal");
		OmsCharger charger_query = new OmsCharger();
		List<OmsCharger> list = omsChargerDao.findListByPageAndGroupAndOrderAndCondition(charger_query, 
				"where forward_srv_id = 0 or forward_srv_id is null", StringUtil.isNumeric(pageNo)?Integer.valueOf(pageNo):null, 
				StringUtil.isNumeric(pageSize)?Integer.valueOf(pageSize):null, 
				StringUtil.isNotBlank(sortName)?sortName:null, 
				StringUtil.isNumeric(sortVal)?Integer.valueOf(sortVal):null, null);
		JsonForList<?> json  = new JsonForList<>(list, "200", "query success");
		return json;
	}
	
	//根据电桩地址查询电桩对应的硬件服务器详情
	@RequestMapping(value="findByChargerId",method=RequestMethod.GET)
	@ResponseBody
	public JsonCommon<?> findByChargerId(HttpServletRequest request, HttpServletResponse response)
	{
		String charger_id = request.getParameter("charger_id");
		String forward_srv_id = request.getParameter("forward_srv_id");
		if(!StringUtil.isNumeric(charger_id)){//MissParam("10303101", "缺少参数字段"),
			return new JsonCommon<>(null, "10303101", "缺少参数字段");
		}
		OmsCharger charger_query = new OmsCharger();
		charger_query.setCharger_id(charger_id);
		OmsCharger charger = chargerServiceImpl.findOneExample(charger_query, null, null);
		if(charger == null||charger.getForward_srv_id()==null||charger.getForward_srv_id()==0){
			return null;
		}
		OmsDicForwardSrvInfo omsDicForwardSrvInfo_query = new OmsDicForwardSrvInfo();
		omsDicForwardSrvInfo_query.setForward_srv_id(Integer.valueOf(forward_srv_id));
		OmsDicForwardSrvInfo omsDicForwardSrvInfo = omsDicForwardSrvInfoServiceImpl.findOneExample(omsDicForwardSrvInfo_query, null, null);
		JsonCommon<?> json  = new JsonCommon<>(omsDicForwardSrvInfo, "200", "query success");
		return json;
	}
}
